package com.tgy.jd.utils;

import com.tgy.jd.entity.Content;
import lombok.extern.slf4j.Slf4j;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLDecoder;
import java.util.ArrayList;


/***
 * @ClassName: HtmlParseUtil
 * @Description: 解析网页
 * @Auther: 送一一块二向箔
 * @Date: 15:32 2022/3/2
 * @version : V1.0
 */
@Slf4j
@Component
public class HtmlParseUtil {
    public static void main(String[] args) throws IOException {
        HtmlParseUtil htmlParseUtil = new HtmlParseUtil();
        ArrayList<Content> java = htmlParseUtil.parseJD("java");
        for (Content content : java) {
            System.out.println(content);
        }
    }


    public ArrayList<Content> parseJD(String keyword)  {
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("https://search.jd.com/Search?keyword=");
        stringBuilder.append(keyword);

        Document document = null;
        try {
            // 1.解析网页(jsoup 解析返回的对象是浏览器Document对象)
            String url = URLDecoder.decode(stringBuilder.toString(), "UTF-8");
            document = Jsoup.parse(new URL(url), 30000);
        } catch (IOException e) {
            log.error("Jsoup解析错误，错误信息{}",e.getMessage());
            e.printStackTrace();
        }
        // 使用document可以使用在js对document的所有操作
        // 2.获取元素（通过id）
        Element j_goodsList = document.getElementById("J_goodsList");
        // 3.获取J_goodsList ul 每一个 li
        Elements lis = j_goodsList.getElementsByTag("li");


        ArrayList<Content> goodsList = new ArrayList<>();
        // 4.获取li下的 img、price、name
        //System.out.println(lis);
        for (Element li : lis) {
            String img = li.getElementsByTag("img").eq(0).attr("data-lazy-img");// 获取li下 第一张图片
            String name = li.getElementsByClass("p-name").eq(0).text();
            String price = li.getElementsByClass("p-price").eq(0).text();

            Content content = new Content(img, name, price);
            goodsList.add(content);
        }

        return goodsList;

    }

}
